#!/bin/bash
#
# Copyright 2007 Kaio Rafael de Souza Barbosa
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# ---------------------------------------------------------
# This Script helps the Subversion Administrations Tasks
# If you want to integrate Apache2 + SVN 
#
# Please, if you need more information about Apache2 and SVN
# read Subversion's Book at
# http://svnbook.red-bean.com/en/1.4/svn.serverconfig.httpd.html
# http://svnbook.red-bean.com/


# Before Use mysvnadmin, please configure it 
# At: /etc/mysvnadmin/mysvnadmin.conf

. /etc/mysvnadmin/mysvnadmin.conf

version=0.3.9.1

# Functions

#
# Help Function
#
usage(){
	echo
	echo "usage: $(basename $0) [options]"
	echo
	echo "Options:"
	echo " "
	echo -e "-c repository \t\t : Create a Subversion repository"
	echo -e "-t template [default|ldap]: Create a repository using a template"
	echo -e "-a username \t\t : Add a user"
	echo -e "-g username \t\t : Same as -a, but system generate a password"
	echo -e "-h \t\t\t : Print this help"
	echo -e "-v \t\t\t : Print mysvnadmin version"
	echo " "

	echo "Examples:"
	echo
	echo "Create a Apache2 based repository"
	echo "mysvnadmin -c FreeSoftware -t default"
	echo
	echo "Add a user with generated password"
	echo "mysvnadmin -g tux"
	echo
}

#
# Create Repository Function
#
create_svn(){
svn_repos=$1 # Get first parameter
template=$2 # Get second parameter

	# Check if $svn_repos exist

	if [ -d ${SVN_ROOT_HOME}/${svn_repos} ];then
		echo "Repository ${svn_repos} already exist"
		exit 1

		else

		if `${SVN_ADM_CMD} create ${SVN_ROOT_HOME}/${svn_repos}` ; then
			echo "Repository: ${svn_repos}, successfully created!"
			else
			echo "Error to create: ${svn_repos}"
			exit 1
		fi
	fi

#
# Change permisson from DAV, LOCK e DB
#

echo "Changing permisson from dav, lock and db"

chmod g+w  ${SVN_ROOT_HOME}/${svn_repos}/dav \
 ${SVN_ROOT_HOME}/${svn_repos}/locks  ${SVN_ROOT_HOME}/${svn_repos}/db

chmod g+s  ${SVN_ROOT_HOME}/${svn_repos}/dav \
 ${SVN_ROOT_HOME}/${svn_repos}/locks  ${SVN_ROOT_HOME}/${svn_repos}/db

chown -R ${APACHE_USER}:${APACHE_GROUP} ${SVN_ROOT_HOME}/${svn_repos}/dav
chown -R ${APACHE_USER}:${APACHE_GROUP} ${SVN_ROOT_HOME}/${svn_repos}/locks
chown -R ${APACHE_USER}:${APACHE_GROUP} ${SVN_ROOT_HOME}/${svn_repos}/db

#
#  Permisson Output
#

cat << EOF > ${SVN_ACCESS_DIR}/${svn_repos}
[groups]
develop =
cm = 

[/]
@develop = r

EOF

# 
# Apache Template
#

if [ "$template" = default ];then

cat << EOF > $SVN_TMP
###########################
#   Project: ${svn_repos}
###########################
<Location ${SVN_WEB_PAGE}/${svn_repos}>
   DAV svn
   SVNPath ${SVN_ROOT_HOME}/${svn_repos}
   AuthType Basic
   AuthName "${svn_repos} in ${COMPANY_NAME} "
   AuthUserFile ${SVN_PASS_FILE}
   AuthzSVNAccessFile ${SVN_ACCESS_DIR}/${svn_repos}
   Require valid-user
   Allow from all
</Location>
EOF

# 
# LDAP Template
#

elif [ "$template" = "ldap" ]; then

cat << EOF > $SVN_TMP
###########################
#   Project: ${svn_repos}
###########################
<Location ${SVN_WEB_PAGE}/${svn_repos}>
   DAV svn
   SVNPath ${SVN_ROOT_HOME}/${svn_repos}
   AuthType Basic
   AuthName "${svn_repos} in ${COMPANY_NAME} "
   AuthBasicProvider ldap
   AuthzLDAPAuthoritative on
   AuthLDAPURL ldap://IP_SERVER:3268/DC=mysvnadmin,DC=org?samAccountName?sub?(objectCategory=*)
   AuthLDAPBindDN "CN=mysvnuser,CN=Users,DC=mysvnadmin,DC=org"
   AuthLDAPBindPassword mysvnpassword
   AuthzSVNAccessFile ${SVN_ACCESS_DIR}/${svn_repos}
#   SVNIndexXSLT "/svnindex.xsl"
   Require ldap-user
   Allow from all
</Location>
EOF

fi


#
# Generating all Apache2 configuration
#

echo "You can find this project at: ${APACHE_PROJECT_CONF}"

echo "# This configuration was generated by mysvnamdin - `date`" \
>> ${APACHE_PROJECT_CONF}/${svn_repos}

cat $SVN_TMP >> ${APACHE_PROJECT_CONF}/${svn_repos}

if [ ! -f ${APACHE_SERVER_CONF}/${svn_repos} ];then
	echo "I'll create the apache2 server symlink"
	echo
	ln -s ${APACHE_PROJECT_CONF}/${svn_repos} ${APACHE_SERVER_CONF}/${svn_repos}
	else
		echo "Error creating a symlink to ${APACHE_SERVER_CONF}/${svn_repos}"
		echo "Please check your Apache2 server configuration"
		exit 1
fi

echo "You need restart or reload your apache2 server!!!"


}

#
# Search user function
#
search_svnuser() {
 	if [ "$(cut -f1 -d: ${SVN_PASS_FILE} | grep "\<${1}\>" )" = $1 ]; then
		echo "User: $1, already exist"
		exit 1
	fi
}

#
# Add user function
#
adduser_svn(){
	search_svnuser $1
	echo "Creating User: $1"
	${APACHE_HTPASS_CMD} ${SVN_PASS_FILE} $1
}

#
# Add user with a generated password
#
adduser_genpass(){
	search_svnuser $1
	echo "Creating User: $1"
	${APACHE_HTPASS_CMD} -b ${SVN_PASS_FILE} $1 ${AUTO_PASSWD}
	echo "User $1 password is: ${AUTO_PASSWD}"
}

#
# Check Variables config
#
if [ ! -f ${SVN_PASS_FILE} ];then
	echo 
	echo "File: ${SVN_PASS_FILE} don't exist"
	echo "You should change the variable  \${SVN_PASS_FILE}"
	echo 
	exit 1
fi

if [ ! -f ${APACHE_HTPASS_CMD} ];then
	echo 
	echo "File:  ${APACHE_HTPASS_CMD} don't exist"
	echo "You should change the variable \${APACHE_HTPASS_CMD}"
	echo 
	exit 1
fi

if [ ! -d ${SVN_ACCESS_DIR} ] ;then
	echo 
	echo "Directory: ${SVN_ACCESS_DIR} don't exist"
	echo "You should change the variable  \${SVN_ACCESS_DIR}"
	echo 
	exit 1
fi

if [ ! -d ${SVN_ROOT_HOME} ] ;then
	echo 
	echo "Directory: ${SVN_ROOT_HOME} don't exist"
	echo "You should change the variable \${SVN_ROOT_HOME}"
	echo 
	exit 1
fi

if [ ! -d ${APACHE_SERVER_CONF} ] ;then
	echo 
	echo "Directory: ${APACHE_SERVER_CONF} don't exist"
	echo "You should change the variable \${APACHE_SERVER_CONF}"
	echo 
	exit 1
fi



# 
# Main Program
#
if [ $(id -u) -eq 0 ];then
	while getopts 'a:c:t:g:vh' Option
		do
        		case $Option in
        		  a ) username=$OPTARG
			      adduser_svn $username ;; 
        		  c ) repos_name=$OPTARG ;;
        		  t ) template=$OPTARG
				if [ -z ${repos_name} ];then
				echo "You need to use -c with -t"
				exit 1
			        fi
			      create_svn $repos_name $template;;  
        		  g ) username=$OPTARG
				adduser_genpass $username;;
        		  v ) echo "$0 $version";;
        		  h) usage;;
        		  \? ) usage
			  	break;;
        		esac
		done
	shift $(($OPTIND -1))
	else
	echo "You need root permission"
	exit 1
fi
